package cn.myallways.iglp.web.controller.mdm;


import com.allways.dubbo.mdm.beans.dict.Dict;
import com.allways.dubbo.mdm.exception.MdmException;
import com.allways.dubbo.mdm.service.dict.IDictService;
import cn.myallways.iglp.constants.BusiConstants;
import com.myallways.iglp.cc.web.entity.ReturnVO;
import com.myallways.iglp.framework.base.controller.BaseController;
import com.myallways.iglp.framework.base.pagination.DataTableRequest;
import com.myallways.iglp.framework.base.pagination.DataTableResponse;
import com.myallways.iglp.framework.base.pagination.PageInfo;
import com.myallways.iglp.framework.base.pagination.PaginationResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典信息Controller
 * Created by weih on 2016-10-11.
 */
@Controller
@RequestMapping("/mdm/dict")
public class DictController extends BaseController {

    @Autowired
    IDictService dictService;
    /**
     * 跳转新增数据字典页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAddPage(){
        return "mdm/dict/add";
    }
    /**
     * 新增数据字典信息
     * @param dict 数据字典对象
     * @return
     */
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public void addDict(@RequestBody Dict dict,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        dict.setCreateUser(getSessionUser().getUserId());
        dict.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dictService.addDict(dict);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转编辑数据字典页面
     * @param dictCode  数据字典代码
     * @return
     */
    @RequestMapping("/toEdit")
    public String toEditPage(String dictCode,Model model){
        model.addAttribute("dict", dictService.getDictByCode(dictCode));
        return "mdm/dict/edit";
    }

    /**
     * 编辑数据字典信息
     * @param dict
     * @return
     */
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public void editDict(@RequestBody Dict dict,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        Dict oldDict=dictService.getDictByCode(dict.getDictCode());
        oldDict.setDictType(dict.getDictType());
        oldDict.setDictCode(dict.getDictCode());
        oldDict.setDictName(dict.getDictName());
        oldDict.setDictDesc(dict.getDictDesc());
        oldDict.setOrderId(dict.getOrderId());
        oldDict.setStatus(dict.getStatus());
        oldDict.setUpdateUser(getSessionUser().getUserId());
        oldDict.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dictService.updateDict(oldDict);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response,vo);
    }

    /**
     * 删除数据字典信息
     * @param dict 数据字典对象
     * @return
     */
    @RequestMapping(value= "/delete", method = RequestMethod.POST)
    public void deleteDict(@RequestBody Dict dict,HttpServletResponse response){
        ReturnVO vo = new ReturnVO();
        vo.setStatus(HttpServletResponse.SC_OK);
        dict.setUpdateUser(getSessionUser().getUserId());
        dict.setDataSource(BusiConstants.DATASOUCE_CODE_FOR_MDM);
        try {
            dictService.deleteDict(dict);
        }catch (MdmException e){
            vo.setStatus(e.getCode());
            vo.setMessage(e.getMessage());
        }
        super.ajaxJson(response, vo);
    }

    /**
     * 跳转分页查询界面
     * @return
     */
    @RequestMapping("/list")
    public String toListPage(){
        return "mdm/dict/list";
    }

    /**
     * 分页查询数据
     * @param dict 数据字典对象
     * @param dtrequest 页码
     */
    @RequestMapping("/getListData")
    @ResponseBody
    public DataTableResponse<Dict> getDictData(Dict dict ,DataTableRequest dtrequest){
        int cnt = (int)dictService.getDictCount(dict);
        List<Dict> list = dictService.queryDicts(dict, dtrequest.getiDisplayStart(), dtrequest.getiDisplayStart() + dtrequest.getiDisplayLength());
        PageInfo pi = new PageInfo();

        pi.setTotal(cnt);
        pi.setRows(dtrequest.getiDisplayLength());
        pi.setPage(dtrequest.getCurrentPage());
        PaginationResult<Dict> result = new PaginationResult<Dict>(list, pi);
        return new DataTableResponse<Dict>(dtrequest.getsEcho(),result);
    }

}
